package com.etonedu.meals.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.etonedu.meals.bean.CardLog;
import com.etonedu.meals.dao.DaoUtils;
import com.etonedu.meals.utils.Utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import io.objectbox.Box;
import io.objectbox.BoxStore;

public class CardLogService extends Service {

    private UploadThead uploadThead;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        upload();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    private void upload() {
        uploadThead = new UploadThead();
        uploadThead.start();
    }

    private class UploadThead extends Thread {
        @Override
        public void run() {
            super.run();
            while (true) {

                try {
                    Box<CardLog> box = DaoUtils.getBoxCardLog();
                    CardLog cardLog = box.query().build().findFirst();

                    JsonObject jsonObject = new JsonObject();

                    JsonArray array = new JsonArray();

                    JsonObject json = new JsonObject();

                    JsonObject object = new JsonObject();
                    object.addProperty("DEVICE_CODE",cardLog.getDevice_code());
                    object.addProperty("SCHOOL_NAME",cardLog.getSchool_name());
                    object.addProperty("LOG_CONTENT",cardLog.getLog_content());
                    object.addProperty("CLASS_NAME",cardLog.getClass_name());
                    object.addProperty("LINE_NUMBER",cardLog.getLine_number());
                    object.addProperty("APP_VERSION_CODE",cardLog.getApp_version_code());
                    object.addProperty("APP_VERSION_NAME",cardLog.getApp_version_name());
                    object.addProperty("LOG_TIME",cardLog.getLog_time());
                    object.addProperty("UID",cardLog.getUid());
                    json.add("value",object);

                    array.add(json);

                    jsonObject.add("records",array);

//                        LogUtils.e("jsonObject = "+jsonObject.toString());

                    RequestParams params = new RequestParams("http://daq.woniukid.cn:80/topics/dev_bp_msg_err");
                    params.setBodyContent(jsonObject.toString());

                    String result = x.http().postSync(params,String.class);

                    LogUtils.e("result = "+result);

                    box.remove(cardLog);

                } catch (Exception e) {

                } catch (Throwable throwable) {

                }

                try {
                    Thread.sleep(3 * 1000);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
